define(function(require, exports, module)
{
    var $ = require("$");
    require("bootstrap_commonrequire");
    require("moment");
    require("bootstrap-daterangepicker");
    
    require("bootstrap");

    require("bootstrap.css");
    require("bootstrap-responsive.css");
    require("theme-colors.css");
    
    require("daterangepicker.css");
    
    /*监听窗口大小变化，增强jquery的resize方法，可以激情dom元素*/
    require('js/core/jquery/src/jquery_resize');
    
    /*添加ztree引用*/
    require("zTreeStyle.css");
	  require("ztreeCore");
	  require("ztreeExcheck");
    
    require('select2.css');
    require('select2');
    var store = require('store');
    var bootbox = require('bootbox');
    
    exports.init = function(param)
    {
        
        $(document).ready(function()
        {
       
    		
    		//获取查询条件
    		function getCondition() {
    		  
    		  var carrierid = $('#carrierid').text();
    		  
    			var isvid = $('#isvid').text();
    			
    			var applyid = $('#applicationid').text();
    			
    			var sessionid = $('#sessionid').text();
    			
    			var queryStr = "";
    			
    			if(carrierid != '' && carrierid != null) {
    				queryStr += ("corpid=" + carrierid + ";");
    			}

    			if(isvid != '' && isvid != null) {
    				queryStr += ("isvid=" + isvid + ";");
    			}
    			
    			if(applyid != '' && applyid != null) {
    				queryStr += ("applyid=" + applyid + ";");
    			}
    			
    			if(sessionid != '' && sessionid != null) {
            queryStr += ("sessionId=" + sessionid + ";");
          }
    			
    			 var daterange = daterangeCom.getPickerRange();
           if (daterange)
           {
             var daterange = daterange.split("/");
             var startTime = daterange[0];
             var endTime = daterange[1];
             queryStr += "startTime=" + startTime + ";"
             queryStr += "endTime=" + endTime+ ";";
           }
    			
    			return queryStr;
    		}
            
            // 初始化日期范围选择控件
    		var DateRange = require('daterangeinput');
    		var daterangeAttrs = {
    			daterangeId : "querydatewrapper_rang",
    			label : "日期"
    		};
    		var daterangeCom = new DateRange({
    			attrs : daterangeAttrs,
    			renderTo : 'querydatewrapper'
    		});
    		
    	  daterangeCom.setRangeToToday();  
    			
    		//加载表单模板和数据
    		var Table = require("table");
            new Table(
            {
                attrs :
                {
                    id : "datatableid",
                    isHovered : false,
                    isStriped : true,
                    isBordered : false
                },
                renderTo : param.divWrapperId
               
            });
            
            var dtUtil = require('datatableutil');
            
            var datatable = $('#datatableid').dataTable({
                   "sDom" : "<'hidden'<'span6'l><'hidden'f>r>t<'row-fluid'<'span6'i><'span6'p>>",
                   "bJQueryUI" : false,
                   "oLanguage" :
                   {
                       "sUrl" : "/business/js/common/table/src/lang/dataTable.cn.txt"
                   },
                   "sPaginationType" : "bootstrap", // 分页自带两种样式，full_numbers和two_button(默认)
                   'bPaginate' : true, // 是否分页
                   "bProcessing" : true, // 当datatable获取数据时候是否显示正在处理提示信息。
                   "bServerSide" : true, // 指定服务器端分页
                   "bDeferRender" : true,
                   "bFilter" : true,
                   "bSort" : true,
                   "bInfo" : true,
                   "sAjaxSource" : "/data/queryPhoneBill.do", // 数据源
                   "iDisplayLength" : DATATABLES_CONFIG.iDisplayLength.DEFAULT,  // 默认分页的每页记录数
                   "fnServerData" : function(sSource, aoData, fnCallback, oSettings)
                   {
                	   
                       var queryparam = dtUtil.convert_aoData(aoData);
                       
                       //拼接where查询条件
                       var cond = getCondition();
                       if(cond != '' && cond != null) {
                       	queryparam.q += cond;
                       }
                        
                       // 添加自己的额外参数
                       oSettings.jqXHR = $.ajax(
                       {
                           "dataType" : 'json',
                           "contentType" : "application/json;charset=UTF-8",
                           "type" : "GET",
                           "url" : sSource,
                           "data" : queryparam, // 将请求数据转换为后台要求的JSON格式
                           "success" : function(json)
                           {
                               dtUtil.successCallback(json, aoData, fnCallback);
                           }
                       });
                       
                   },
                   "aoColumns" : [
                     
                    {
                      "sTitle":"会话ID",
                      "bSortable":false,
                      "mData":"sessionid",
                      "sWidth" : "15%",
                        "bVisible":true
                    },
                    {
                    	"sTitle":"通话开始时间",
                    	"bSortable":false,
                    	"mData":"callingtime",
                    	 "sWidth" : "15%"
                    },
                    {
                        "sTitle" : "通话时长(分钟)",
                        "bSortable" : false,
                        "mData" : "callingduration",
                        "sWidth" : "10%"
                    },
                   
                    {
                        "sTitle" : "主叫号",
                        "bSortable" : false,
                        "mData" : "callingnum",
                        "sWidth" : "10%"
                    },
                    
                    {
                        "sTitle" : "被叫号数量",
                        "bSortable" : false,
                        "mData" : "callednum",
                        "sWidth" : "20%",
                        "mRender": function(data, type, full)
          					{
          						var ret = '<div class="btn-group" style="margin-top: 8px;">';
          						ret = ret + '<a href="javascript:void(0);" class="btn btn-link" style="text-decoration:underline;margin-top:-16px;" title="明细" aid=' + data + '>'+full.callednum+'</a>';
          						return ret += '</div>';
          					}
                    }],
               	"fnDrawCallback": function( oSettings ) {
                   	// 表格绘制完成后，去除width属性，使表格可缩放
                   	$('#'+oSettings.sTableId).css("width", "");
                   }
             });
            
          //明细操作事件
			$("#datatableid tbody").on("click", "tr>td>div>a.btn-link", function()
			{
				var rowNode = this.parentNode.parentNode.parentNode;
				var rowData = datatable.fnGetData(rowNode);
				
				/*$("#queryAreaId").hide();
				$("#data_table").hide();
				$("#data_table_detail").show();
				
				$("#back_btn").show();
				$("#show").text("被叫号码详细信息");
				var applymodify = require("js/common/pub/src/js/phonebilldetail.js");
				applymodify.init("pub/phonebill_m.jsp","data_table_detail", rowData,param.flagIsv);*/
				
				window.location.href="/userpage/queryBillDetail.do?callingnum="+
				rowData.callingnum+"&sessionid="+rowData.sessionid+"&callingduration="+rowData.callingduration;

			});
            
      $('#btnquery').click(function(){
      	
         var startyear = $("#startyearid").val();
   	   var startmonth = $("#startmonthid").val();
   	   var endyear = $("#endyearid").val();
   	   var endmonth = $("#endmonthid").val();
   	   if(startyear > endyear){
   		   bootbox.alert("所选起始年份大于终止年份，请重新选择！");
   		   return false;
   	   }else if(startyear == endyear){
   		   if(startmonth > endmonth){
   			   bootbox.alert("所选起始月份大于终止月份，请重新选择！");
   			   return false;
   		   }
   	   }
      	
      	datatable.fnDraw();
      	
      	return false;
      }
      ); 

         
           
        });
        
    };
});

